package com.offcn.controller;

import com.github.pagehelper.PageInfo;
import com.offcn.bean.Evaluate;
import com.offcn.bean.User;
import com.offcn.service.EvaluateService;
import com.offcn.utils.ReturnResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("evaluate")
@ResponseBody
public class EvaluateController {
    private ReturnResult returnResult;
    @Autowired
    private EvaluateService evaluateService;

    //发表评论
    @RequestMapping("addEvaluate")
    public ReturnResult addEvaluate(Evaluate evaluate, HttpSession session){
        User user = (User) session.getAttribute("userSession");
        evaluate.setuFk(user.getUid());
        evaluate.setEtime(new Date());
        try {
            evaluateService.addEvaluate(evaluate);
            returnResult = new ReturnResult("评论成功!",1,null);
        } catch (Exception e) {
            e.printStackTrace();
            returnResult = new ReturnResult("评论失败!",-1,null);
        }
        return returnResult;
    }

    //删除评论
    @RequestMapping("deleteComment")
    public ReturnResult deleteComment(Integer eid){
        try {
            evaluateService.deleteComment(eid);
            returnResult = new ReturnResult("删除成功!",1,null);
        } catch (Exception e) {
            e.printStackTrace();
            returnResult = new ReturnResult("删除失败!",-1,null);
        }
        return returnResult;
    }

    //查看博客详情下的评论
    @RequestMapping("getEvaluate")
    public PageInfo<Evaluate> getEvaluate(@RequestParam(value = "bid",defaultValue = "-1") Integer bid,
                                          @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                          @RequestParam(value = "econtent",defaultValue = "") String econtent){
        PageInfo<Evaluate> evaluate = evaluateService.getEvaluate(bid, pageNo,econtent);
        return evaluate;
    }
}
